include("SupportFunctions");
include("Civ6Common");
include("InstanceManager");
include("PopupDialog");
include("AnimSidePanelSupport");
include( "CitySupport" );

local m_TestGreatWriterX = GlobalParameters.LADICA_GREAT_WRITER_X
local m_TestGreatWriterY = GlobalParameters.LADICA_GREAT_WRITER_Y
local greatWriID = GameInfo.GreatPersonClasses['GREAT_PERSON_CLASS_WRITER'].Index

-- local m_TestGreatWriter = GlobalParameters.LADICA_GREAT_WRITER
-- local conqueredCities = {}
-- --征服城市获得20大作点，不会重复获得，被顾问抛弃了
-- function isCityConquered(cityID)
--     for i, id in ipairs(conqueredCities) do
--         if id == cityID then
--             return true
--         end
--     end
--     return false
-- end

-- function TestCityConquered(newPlayerId, newCityId, iCityX, iCityX)
--     local player = Players[newPlayerId]
--     local civType = PlayerConfigurations[newPlayerId]:GetCivilizationTypeName()
--     print(civType)
--     if civType == "CIVILIZATION_KARA_KHANID" and not isCityConquered(newCityId) then
--         ExposedMembers.GameEvents.SekkaGetGreatPeoplePointsSwitch.Call(newPlayerId, greatWriID, 20)
--         table.insert(conqueredCities, newCityId)
--         -- print(conqueredCities,newCityId)
--     end
-- end

-- function TestCityBuilt(playerID, cityID, iX, iY)
--     local player = Players[playerID]
--     local civType = PlayerConfigurations[playerID]:GetCivilizationTypeName()
--     if civType == "CIVILIZATION_KARA_KHANID" and not isCityConquered(newCityId) then
--                 -- ExposedMembers.GameEvents.SekkaGetGreatPeoplePointsSwitch.Call(newPlayerId, greatWriID, 20)
--             table.insert(conqueredCities, cityID)
--             -- print(conqueredCities,cityID)
--     end
    
-- end
-- GameEvents.CityConquered.Add(TestCityConquered)
-- Events.CityInitialized.Add(TestCityBuilt)


-- 新方案

function TestCityProductionCompleted(playerID, cityID, iConstructionType, itemID, bCancelled)
    local player = Players[playerID]
    if player:IsHuman() then        
        if iConstructionType == 0 then
            local unit = GameInfo.Units[itemID]
            if unit ~= nil then
                print("单位建造完成: " .. unit.Name)
            end
            local civType = PlayerConfigurations[playerID]:GetCivilizationTypeName()
                if civType == "CIVILIZATION_KARA_KHANID" then
                            local city = CityManager.GetCity(playerID, cityID)
                            local Dyncost = city:GetBuildQueue():GetUnitCost(unit.Index)
                            -- print (Dyncost)
                            local gameSpeed = GameConfiguration.GetGameSpeedType()
                            local iSpeedCostMultiplier = GameInfo.GameSpeeds[gameSpeed].CostMultiplier * 0.01
                            local amountX = Dyncost * m_TestGreatWriterX * 0.01 * iSpeedCostMultiplier
                            local amountY = Dyncost * m_TestGreatWriterY * 0.01 * iSpeedCostMultiplier
                    print(civType)
                        if (unit.Combat) and (unit.Combat > 0) then

                -- 生产的是军事单位，其造价的10%转大作点
                        ExposedMembers.GameEvents.SekkaGetGreatPeoplePointsSwitch.Call(playerID, greatWriID, amountX)
                        else
                            -- 生产的是平民单位，其造价的10%转大作点
                        ExposedMembers.GameEvents.SekkaGetGreatPeoplePointsSwitch.Call(playerID, greatWriID, amountY)

                        end
                    

                end
        end
    end
end

Events.CityProductionCompleted.Add(TestCityProductionCompleted)


function TestCityMadePurchase(playerID, cityID, iX, iY, iPurchaseType, iObjectType)
    local player = Players[playerID]
    local civType = PlayerConfigurations[playerID]:GetCivilizationTypeName()

    if (civType == "CIVILIZATION_KARA_KHANID") and (iPurchaseType == EventSubTypes.UNIT) then
        local unit = GameInfo.Units[iObjectType]
        if (unit.ReligiousStrength > 0) then 
            return
        end
        local city = CityManager.GetCity(playerID, cityID)
        local Dyncost = city:GetBuildQueue():GetUnitCost(unit.Index)
        local gameSpeed = GameConfiguration.GetGameSpeedType()
        local iSpeedCostMultiplier = GameInfo.GameSpeeds[gameSpeed].CostMultiplier * 0.01
        local amountX = Dyncost * m_TestGreatWriterX * 0.01 * iSpeedCostMultiplier
        local amountY = Dyncost * m_TestGreatWriterY * 0.01 * iSpeedCostMultiplier
        if (unit.Combat) and (unit.Combat > 0) then
                -- 购买的是军事单位，其造价的10%转大作点
            ExposedMembers.GameEvents.SekkaGetGreatPeoplePointsSwitch.Call(playerID, greatWriID, amountX)
        else

                            -- 购买的是平民单位，给玩家增加x点大文
            ExposedMembers.GameEvents.SekkaGetGreatPeoplePointsSwitch.Call(playerID, greatWriID, amountY)
        end
    end
end

Events.CityMadePurchase.Add(TestCityMadePurchase)